home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Control Common / CCFragResource.cpp < prev    next >
C/C++ Source or Header  |  1996-12-21  |  3KB  |  81 lines

  1. #include <LowMem.h>
  2. #include "ResourceMap.h"
  3. #include "DllEntry.h"
  4. #include "CCFragResource.h"
  5.  
  6.  
  7. long                CCFragResource::EntryCount = 0;
  8. ResourceMapHandle    CCFragResource::CFragTopMapHdl = NULL;
  9. ResourceMapHandle    CCFragResource::ContainerTopMapHdl = NULL;
  10. ResourceMapHandle    CCFragResource::DLLTopMapHdl = NULL;
  11. short                CCFragResource::CFragResRefNum = -1;
  12. short                CCFragResource::ContainerResRefNum = -1;
  13.  
  14.  
  15. CCFragResource::CCFragResource()
  16. {
  17.     if (EntryCount++ == 0)
  18.     {
  19.         //    if we are coming through the first time then
  20.         //    ask the code fragment/dll about its initial resource state
  21.         if (DLLTopMapHdl == NULL)
  22.             DLLTopMapHdl = ::GetDllTopMapHdl();
  23.         if (CFragTopMapHdl == NULL)
  24.             CFragTopMapHdl = DLLTopMapHdl;
  25.         if (CFragResRefNum == -1)
  26.             CFragResRefNum = ::GetDllResRefNum();
  27.  
  28.         //    save off the current resource state for latter
  29.         ContainerTopMapHdl = ResourceMapHandle( ::LMGetTopMapHndl() );
  30.         ContainerResRefNum = ::CurResFile();
  31.  
  32.         //    test to see if the resource chain is consistent
  33.         //    if the container closed the resource file that was at the top of the resource
  34.         //        chain when the DLL was opened then the DLL's NextResourceMap handle
  35.         //        is invalid, and trying to put the DLL's resource map record on the top
  36.         //        of the chain could be REALLY BAD NEWS.
  37.         //    we can detect this condition by checking to see if DLL->NextResourceMap is
  38.         //        in the resource map chain. we attempt to put set DLL->NextResourceMap
  39.         //        to the current top of the chain if the unthinkable has happened.
  40.         //        Unfortunately for us, it doesn't always work.
  41.  
  42.         Boolean                Found = false;
  43.         ResourceMapHandle    TempHandle = ContainerTopMapHdl;
  44.         ResourceMapHandle    Target = (*DLLTopMapHdl)->NextResourceMap;
  45.         ResourceMapHandle    Stop = ResourceMapHandle( ::LMGetSysMapHndl());
  46.  
  47.         while (!(Found = TempHandle == Target) &&
  48.             (TempHandle = (*TempHandle)->NextResourceMap) != Stop)
  49.             ;
  50.  
  51.         if (!Found)
  52.         {
  53.             //    WARNING - WARNING - WARNING
  54.             //    this MAY crash
  55.             //    send a debugger message to let everyone know.
  56.             DebugStr("\pWarning - Container closed resource DLL was pointing to.");
  57.             (*DLLTopMapHdl)->NextResourceMap = ContainerTopMapHdl;
  58.         }
  59.  
  60.         //    set up the code fragment/dll's resource state
  61.         ::LMSetTopMapHndl( Handle(CFragTopMapHdl) );
  62.         ::UseResFile(CFragResRefNum);
  63.     }
  64. }
  65.  
  66.  
  67. CCFragResource::~CCFragResource()
  68. {
  69.     if (--EntryCount == 0)
  70.     {
  71.  
  72.         //    save the code fragment/dll's resource state
  73.         CFragTopMapHdl = ResourceMapHandle( ::LMGetTopMapHndl() );
  74.         CFragResRefNum = ::CurResFile();
  75.  
  76.         //    restore the resource state of the container
  77.         ::LMSetTopMapHndl( Handle(ContainerTopMapHdl) );
  78.         ::UseResFile(ContainerResRefNum);
  79.     }
  80. }
  81.